﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QunProject.Common;

namespace QunProject.Interface
{
    /// <summary>
    /// 接口基类
    /// </summary>
    public class BaseInterface
    {
        /// <summary>
        /// 缓存名称
        /// </summary>
        public static class CacheName
        {
            /// <summary>
            /// 首页缓存
            /// </summary>
            public static readonly string GetHomeIndexResult_Cache = "GetHomeIndexResult_Cache";
            /// <summary>
            /// 推荐文章缓存
            /// </summary>
            public static readonly string GetHotArtucleResult_Cache = "GetHotArtucleResult_Cache";
            /// <summary>
            /// 友情链接缓存
            /// </summary>
            public static readonly string GetFriendHrefResult_Cache = "GetFriendHrefResult_Cache";
            /// <summary>
            /// 推荐技术讨论缓存
            /// </summary>
            public static readonly string GetHotDiscussResult_Cache = "GetHotDiscussResult_Cache";
            /// <summary>
            /// 推荐专题缓存
            /// </summary>
            public static readonly string GetHotSpecialResult_Cache = "GetHotSpecialResult_Cache";

            /// <summary>
            /// 用户讨论列表缓存[需要加上用户ID]
            /// </summary>
            public static readonly string GetMemberReplyListResult_Cache = "GetMemberReplyListResult_Cache";
        }

        /// <summary>
        /// EF操作实例
        /// </summary>
        public Model.QunProjectEntities EF = new Model.QunProjectEntities();
        /// <summary>
        /// 返回数据对象
        /// </summary>
        public Common.Result Result = new Common.Result();
        /// <summary>
        /// 缓存操作对象
        /// </summary>
        public Cache.ICache Cache = QunProject.Cache.ExampleCache.GetExampleCache();
        /// <summary>
        /// 短信操作对象
        /// </summary>
        public Common.SmsHelper Sms = new Common.SmsHelper();
        /// <summary>
        /// ToKen（须每次调用接口时传入）
        /// </summary>
        public string ToKen
        {
            get
            {
                string token = System.Web.HttpContext.Current.Request["token"];
                if (token.IsNullOrWhiteSpace())
                {
                    throw new Exception(Common.ConstantCode.Result.result_login_outtime);
                }
                return token;
            }
        }
        /// <summary>
        /// 登录用户
        /// </summary>
        public Model.member LoginMember
        {
            get
            {
                var _userCache = Cache.Get(ToKen);
                if (_userCache == null)
                {
                    throw new Exception(Common.ConstantCode.Result.result_login_outtime);
                }
                return (Model.member)_userCache;
            }
        }
        /// <summary>
        /// 登录用户ID
        /// </summary>
        public long MemberID
        {
            get
            {
                return LoginMember.ID;
            }
        }
        /// <summary>
        /// 判断是否登录
        /// </summary>
        /// <returns></returns>
        public bool IsLogin()
        {
            string token = System.Web.HttpContext.Current.Request["token"];
            if (token.IsNullOrWhiteSpace())
                return false;
            else
            {
                var _userCache = Cache.Get(ToKen);
                if (_userCache == null)
                    return false;
                else
                    return true;
            }
        }
    }
}
